/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import main.CommonMethods;

import org.apache.log4j.Logger;

import utopiaintelpackage.BaseParser;
import botobjects.NewsItem;

import commandhandlers.NewsHandler;

/**
 * A class that communicates with the forum agent site to save intel
 * 
 * 
 */
public class IntelHost implements Runnable {
   private final Socket socket;

   public IntelHost(final Socket socket) {
	  super();
	  this.socket = socket;
   }

   @Override
   public void run() {
	  BufferedReader in = null;
	  PrintWriter out = null;
	  try {
		 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		 String read = in.readLine().trim();

		 if ("Sending intel".equals(read)) {
			final String user = in.readLine();
			if (user != null) {
			   final String pass = in.readLine();
			   if (pass != null) {
				  out = new PrintWriter(socket.getOutputStream(), true);
				  if (CommonMethods.loginBotAccount(user, pass)) {
					 out.write("Authentication successful\n");
					 out.flush();
					 List<String> intel = new ArrayList<String>();
					 String line = "";
					 while ((line = in.readLine()) != null) {
						if ("Done".equals(line)) {
						   break;
						}
						intel.add(line);
					 }
					 for (final String s : BaseParser.INSTANCE.parseIntel(intel, user)) {
						out.write(s + "\n");
						out.flush();
					 }
					 out.write("Finished\n");
					 out.flush();
				  } else {
					 out.write("Authentication failed\n");
					 out.flush();
					 out.write("Finished\n");
					 out.flush();
				  }
			   }
			}
		 } else if ("Sending news".equals(read)) {
			final String user = in.readLine();
			if (user != null) {
			   final String pass = in.readLine();
			   if (pass != null) {
				  out = new PrintWriter(socket.getOutputStream(), true);
				  if (CommonMethods.loginBotAccount(user, pass)) {
					 List<String> news = new ArrayList<String>();
					 String line = "";
					 while ((line = in.readLine()) != null) {
						line = line.trim();
						if ("Done".equals(line)) {
						   break;
						}
						if (NewsHandler.isNews(line))
						   news.add(line);
					 }
					 boolean success = !news.isEmpty() && NewsItem.saveBatch(NewsHandler.parsePastedNews(news));
					 out.write(success ? "Success\n" : "Failed\n");
					 out.flush();
				  } else {
					 out.write("Authentication failed\n");
					 out.flush();
				  }
			   }
			}
		 }
	  } catch (final Exception e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			in.close();
		 } catch (final Exception e) {
		 }
		 try {
			out.close();
		 } catch (final Exception e) {
		 }
		 try {
			socket.close();
		 } catch (final Exception e) {
		 }
	  }
   }
}
